package controller;

import bean.GeneralBean;
import bean.ItemBean;
import begin.MainView;
import begin.UserMenu;
import dao.GeneralDao;
import dao.UserDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;

import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import service.LuckService;
import service.ShopService;
import service.UserService;
import tools.UtilData;
import tools.UtilMessage;

/**
 * @Author 王志康
 * @Date 2021/9/6 9:22
 * @Version 1.0.0
 */
public class UserMenuController {

    //抽奖页面

    @FXML
    private Label integralNow;
    @FXML
    private Label luckResult;
    @FXML
    private PieChart pirChart;

    //购物清单

    @FXML
    public Label allPriceLabel;
    @FXML
    private TableView<ItemBean> shopTable;
    @FXML
    private TableColumn<Object, Object> itemName;
    @FXML
    private TableColumn<Object, Object> itemNum;
    @FXML
    private TableColumn<Object, Object> itemPrice;
    @FXML
    private TableColumn<Object, Object> allPrice;

    //表格-水果

    @FXML
    public TableView<GeneralBean> generalTable;

    @FXML
    private TextField itemId;
    @FXML
    private TextField itemNumber;
    @FXML
    private TableColumn<Object, Object> generalId;
    @FXML
    private TableColumn<Object, Object> generalName;
    @FXML
    private TableColumn<Object, Object> generalPrice;
    @FXML
    private TableColumn<Object, Object> generalArea;
    @FXML
    private TableColumn<Object, Object> generalDate;
    @FXML
    private TableColumn<Object, Object> generalTime;

    //用户信息

    @FXML
    private Label number;
    @FXML
    private Label password;
    @FXML
    private Label name;
    @FXML
    private Label phone;
    @FXML
    private Label integral;
    @FXML
    private TextField phoneChange;
    @FXML
    private TextField passwordChange;

    /**
     * 切换至用户信息选项卡时,获取用户信息并显示
     *
     * @param event 事件
     */
    @FXML
    public void informationClicked(Event event) {
        number.setText(UserMenu.user.getNumber());
        password.setText(UserMenu.user.getPassword());
        name.setText(UserMenu.user.getUserName());
        phone.setText(UserMenu.user.getPhoneNumber());
        integral.setText(String.valueOf(UserMenu.user.getIntegral()));
    }

    /**
     * 切换至商品列表选项卡时,获取水果信息并显示
     *
     * @param event 事件
     */
    public void generalTableClicked(Event event) {
        //数据绑定
        generalId.setCellValueFactory(new PropertyValueFactory<>("productId"));
        generalName.setCellValueFactory(new PropertyValueFactory<>("productName"));
        generalPrice.setCellValueFactory(new PropertyValueFactory<>("productPrice"));
        generalArea.setCellValueFactory(new PropertyValueFactory<>("productArea"));
        generalDate.setCellValueFactory(new PropertyValueFactory<>("dateOfManufacture"));
        generalTime.setCellValueFactory(new PropertyValueFactory<>("qualityGuaranteePeriod"));
        ObservableList<GeneralBean> list = FXCollections.observableArrayList();
        list.addAll(GeneralDao.getGeneralList());
        generalTable.setItems(list);
    }

    /**
     * 切换至购物清单选项卡时,获取清单信息并显示
     *
     * @param event 事件
     */
    public void shopCartClicked(Event event) {
        allPriceLabel.setText(ShopService.countAllPrice() + "元");
        //数据绑定
        itemName.setCellValueFactory(new PropertyValueFactory<>("itemName"));
        itemNum.setCellValueFactory(new PropertyValueFactory<>("itemNumber"));
        itemPrice.setCellValueFactory(new PropertyValueFactory<>("itemPrice"));
        allPrice.setCellValueFactory(new PropertyValueFactory<>("allPrice"));
        ObservableList<ItemBean> list = FXCollections.observableArrayList();
        list.addAll(UserMenu.listItem);
        shopTable.setItems(list);
    }

    /**
     * 切换至抽奖选项卡
     *
     * @param event 事件
     */
    public void luckClicked(Event event) {
        ObservableList<PieChart.Data> pirChartData = FXCollections.observableArrayList(
                new PieChart.Data(UtilData.LUCK_FIRST_STR, UtilData.LUCK_FIRST_PROBABILITY),
                new PieChart.Data(UtilData.LUCK_SECOND_STR, UtilData.LUCK_SECOND_PROBABILITY),
                new PieChart.Data(UtilData.LUCK_THIRD_STR, UtilData.LUCK_THIRD_PROBABILITY),
                new PieChart.Data(UtilData.LUCK_FOURTH_STR, UtilData.LUCK_FOURTH_PROBABILITY)
        );
        pirChart.setTitle("抽奖结果概率公示");
        pirChart.setData(pirChartData);
        integralNow.setText(String.valueOf(UserMenu.user.getIntegral()));
    }

    /**
     * 更改用户手机号
     *
     * @param mouseEvent 鼠标事件
     */
    @FXML
    public void phoneChangeClicked(MouseEvent mouseEvent) {
        String phoneChanged = phoneChange.getText();
        UserService.phoneInformationController(phoneChanged);
        phoneChange.clear();
        phone.setText(UserMenu.user.getPhoneNumber());
    }

    /**
     * 购物清单结算
     *
     * @param mouseEvent 鼠标事件
     */
    public void settlement(MouseEvent mouseEvent) {
        ShopService.settlement();
        //清除购物清单
        shopTable.getItems().clear();
        allPriceLabel.setText("0.0");
    }

    /**
     * 更改用户密码
     *
     * @param mouseEvent 鼠标事件
     */
    @FXML
    public void passwordChangeClicked(MouseEvent mouseEvent) {
        String passwordChanged = passwordChange.getText();
        UserService.passwordInformationController(passwordChanged);
        passwordChange.clear();
        password.setText(UserMenu.user.getPassword());
    }

    /**
     * 添加到购物车
     *
     * @param mouseEvent 鼠标事件
     */
    public void addShopCart(MouseEvent mouseEvent) {
        String id = itemId.getText();
        String numberText = itemNumber.getText();
        ShopService.addShopCart(numberText, id);
        itemId.clear();
        itemNumber.clear();
    }

    /**
     * 系统退出函数
     *
     * @param mouseEvent 鼠标事件
     */
    public void exit(MouseEvent mouseEvent) {
        System.exit(0);
    }

    /**
     * 账号注销函数
     *
     * @param mouseEvent 鼠标事件
     */
    public void cancellation(MouseEvent mouseEvent) {
        Stage stage = (Stage) itemId.getScene().getWindow();
        stage.close();
        new MainView().gotoMainView();
    }

    /**
     * 点击获取表格数据并填充至文本框
     *
     * @param mouseEvent 鼠标事件
     */
    public void generalClicked(MouseEvent mouseEvent) {
        int id = generalTable.getSelectionModel().getSelectedItem().getProductId();
        itemId.setText(String.valueOf(id));
    }

    /**
     * 抽奖
     *
     * @param mouseEvent 鼠标事件
     */
    public void luckDraw(MouseEvent mouseEvent) {
        //更新用户积分数据
        if (UserMenu.user.getIntegral() < UtilData.LUCK_INTEGRAL) {
            new UtilMessage().messageShow("积分不足");
            return;
        }
        int integral = Integer.parseInt(integralNow.getText()) - UtilData.LUCK_INTEGRAL;
        integralNow.setText(String.valueOf(integral));
        UserMenu.user.setIntegral(integral);
        UserDao.updateUser(UserMenu.user);
        String res = LuckService.getRandomStr();
        luckResult.setText(res);
        new UtilMessage().messageShow("恭喜获得" + res);
    }
}
